package com.evil.application.pojo.dto.es;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liyang
 * @date 2023-08-08 15:27
 */
@Data
@Accessors(chain = true)
public abstract class Agg {

    private String title;

    private List<Agg> subs;

    public Agg() {
        this.subs = new ArrayList<>();
    }

    public Agg(String title) {
        this.title = title;
        this.subs = new ArrayList<>();
    }

    public Agg sub(Agg agg) {
        this.subs.add(agg);
        return this;
    }

    public abstract Aggregation.Builder.ContainerBuilder toAggregation();

    public String underline(String str) {
        return StrUtil.toUnderlineCase(str);
    }

    @Override
    public String toString() {
        HashMap<Object, Object> map = new HashMap<>();
        Map<String, Object> objMap = BeanUtil.beanToMap(this, true, false);
        map.put(this.title, objMap);
        return JSONUtil.toJsonStr(map);
    }
}
